/*
* $Id: InspectHForm.java 270 2011-02-20 13:59:40Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;

import apiary.Apiary;
import apiary.component.journal.Wizard;
import apiary.ui.CategoryItem;
import apiary.common.FormHelper;
import apiary.ui.ItemEventListener;

public class InspectHForm extends WizardForm implements ItemEventListener,
                                                        ItemStateListener
{
    private Command addHCommand;
    private Command delHCommand;
    private Command chooseCommand;
    private CategoryItem curCategory;
    private boolean isShowChooseCommand;
    private Vector inspectHValue;

    public InspectHForm(String name, Wizard wiz, Integer id, boolean nextCommand) {
        super(name, wiz, id, nextCommand);
        isShowChooseCommand = false;
        inspectHValue = new Vector();
        Apiary apiary = Apiary.getInstance();
        addHCommand = new Command(apiary.getLocaleString("AddHousing"), Command.SCREEN, 4);
        addCommand(addHCommand);
        delHCommand = new Command(apiary.getLocaleString("DeleteHousing"), Command.SCREEN, 5);
        addCommand(delHCommand);
        chooseCommand = new Command(apiary.getLocaleString("Choose"), Command.ITEM, 0);

        setItemStateListener(this);
    }

    protected void commandUser(Command command) {
        if(command == chooseCommand)
            itemNotifyEvent(curCategory, PRESS);
        else if(command == addHCommand)
            addHousing();
        else if(command == delHCommand)
            deleteHousing();
    }

    public Object GetDataForm() {
        return inspectHValue;
    }

    public void itemNotifyEvent(Item item, String event) {
        if(event.equals(ItemEventListener.PRESS)){
            int number = 0;
            for(; number < size(); number++) {
                if(get(number) == item)
                    break;
            }
            InspectValue inspect = (InspectValue) inspectHValue.elementAt(number);
            InspectForm form = new InspectForm(inspect);
            form.setBackScreen(this);
            Apiary.getInstance().Display(form);
        }
    }

    public void itemStateChanged(Item item) {
        CategoryItem categoryItem = (CategoryItem) item;
        if(categoryItem.getActive())
            curCategory = categoryItem;
        else
            curCategory = null;
        if(size() > 0 && !isShowChooseCommand) {
            addCommand(chooseCommand);
            isShowChooseCommand = true;
        }
        else if(size() <= 0 && isShowChooseCommand) {
            removeCommand(chooseCommand);
            isShowChooseCommand = false;
        }
    }

    public void setInspects(Vector inspects) {
        deleteAll();
        inspectHValue = inspects;
        for(int i = 0; i < inspects.size(); i++) {
            InspectValue inspect = (InspectValue) inspects.elementAt(i);
            String id = String.valueOf(inspect.idHousing);
            CategoryItem item = FormHelper.createCategoryItem(this, id, inspect.nameHousing, "");
            append(item);
        }
    }

    private void addHousing() {
        Apiary apiary = Apiary.getInstance();
        AddInspectForm housing = new AddInspectForm(
                apiary.getLocaleString("AddHousing"), inspectHValue);
        housing.setBackScreen(this);
        apiary.Display(housing);
    }

    private void deleteHousing() {
        for(int i = 0; i < size(); i++) {
            if(get(i) == curCategory) {
                curCategory.setActive(false);
                inspectHValue.removeElementAt(i);
                delete(i);
                break;
            }
        }
    }
}
